package com.wenhai.leetcode.stack;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/min-stack/
 *
 * @author wenhai
 * @date   2021/2/18
 */
public class _155最小栈 {
    private final Stack<Integer> stack;
    private final Stack<Integer> minStack;

    /** initialize your data structure here. */
    public _155最小栈() {
        this.stack = new Stack<>();
        this.minStack = new Stack<>();
    }

    public void push(int x) {
        this.stack.push(x);
        pushMinStack(x);
    }

    private void pushMinStack(int x) {
        if (minStack.isEmpty() || minStack.peek() >= x) {
            minStack.push(x);
        } else {
            minStack.push(minStack.peek());
        }
    }

    public void pop() {
        if (!this.stack.isEmpty()) {
            stack.pop();
            minStack.pop();
        }
    }

    public int top() {
        return this.stack.peek();
    }

    public int getMin() {
        return this.minStack.peek();
    }
}
